<template>
	<!-- 基础环境巡检 -->
	<view>
		<view class="page_top">
			<view class="page_top_conetnt">
				<image src="@/static/logo/back_icon.png" mode="" class="back_logo" @click="back"></image>
				<text>巡检详情</text>
				
				<text class="submit_text" @click="submit" v-if="form.status==0">提交</text>
			</view>
		</view>
		<view class="tabs">
			<u-tabs :list="tabsList" @click="tabsClick" :lineWidth="50"></u-tabs>
		</view>
		<scroll-view scroll-y="true" class="scroll_box">
			<view class="scroll_content">
				<customForm v-model="form" :configList="configList" :disabled="form.status==1"></customForm>
			</view>
		</scroll-view>

		<view class="form-content" v-if="false">
			<uni-forms :modelValue="baseEnvironmentVoForm" label-position="top" label-width="100%" v-if="false">
				<uni-forms-item label="巡检日期" name="inspectTime">
					<uni-datetime-picker type="datetime" :clear-icon="false" :disabled="true"
						v-model="baseEnvironmentVoForm.inspectTime" />
				</uni-forms-item>
				<uni-forms-item label="主机房温湿度" name="computerTemperature">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.computerTemperature"
						placeholder="请输入主机房温湿度" />
				</uni-forms-item>
				<uni-forms-item label="配电室温湿度" name="switchTemperature">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.switchTemperature"
						placeholder="请输入配电室温湿度" />
				</uni-forms-item>
				<uni-forms-item label="弱电间温湿度" name="electricityTemperature">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.electricityTemperature"
						placeholder="请输入弱电间温湿度" />
				</uni-forms-item>

				<uni-forms-item label="各设备间地面、墙面、玻璃幕墙洁净度" name="other">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.other"
						placeholder="请输入各设备间地面、墙面、玻璃幕墙洁净度" />
				</uni-forms-item>
				<uni-forms-item label="机柜情况" name="cabinetCondition">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.cabinetCondition" placeholder="请输入机柜情况" />
				</uni-forms-item>
				<uni-forms-item label="异味、异响" name="abnormalSmell">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.abnormalSmell" placeholder="请输入异味、异响" />
				</uni-forms-item>
				<uni-forms-item label="故障及处理情况" name="treatment">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.treatment" placeholder="请输入故障及处理情况" />
				</uni-forms-item>
				<uni-forms-item label="建议" name="advice">
					<uni-easyinput type="text" v-model="baseEnvironmentVoForm.advice" placeholder="请输入建议" />
				</uni-forms-item>

			</uni-forms>
		</view>
		
		<u-toast ref="uToastRef"></u-toast>

	</view>
</template>

<script setup>
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import {
		inspaServiceNetDetail,
		inspaBaseDetail, getFormInfoAPI, updataFormInfoAPI
	} from '@/api/inspestion.js'
	import { nextTick, reactive, toRefs, ref, computed } from "vue";
	
	import customForm from "./componetns/customForm.vue"
	
	let loop = false //是否重新请求
	const uToastRef = ref(null)
	const configList = computed(()=>{
		if(curBar.value == '1') { // UPS巡检表
			return [
				{ name:"设备型号", key: "modelNumber", type: "text", placeholder:"请输入设备型号", default:"" },
				{ name:"设备序列号", key: "serialNumber", type: "text", placeholder:"请输入设备序列号", default:"" },
				{ name:"运行状态", key: "operation", type: "text", placeholder:"请输入运行或待机", default:"" },
				{ name:"告警信息", key: "alarm", type: "text", placeholder:"请输入是否告警", default:"" },
				{ name:"主路输入电压线A", key: "mainInputA", type: "text", placeholder:"请输入主路输入电压", default:"" },
				{ name:"主路输入电压线B", key: "mainInputB", type: "text", placeholder:"请输入主路输入电压", default:"" },
				{ name:"主路输入电压线C", key: "mainInputC", type: "text", placeholder:"请输入主路输入电压", default:"" },
				{ name:"旁路输入电压线A", key: "sideInputA", type: "text", placeholder:"请输入旁路输入电压", default:"" },
				{ name:"旁路输入电压线B", key: "sideInputB", type: "text", placeholder:"请输入旁路输入电压", default:"" },
				{ name:"旁路输入电压线C", key: "sideInputC", type: "text", placeholder:"请输入旁路输入电压", default:"" },
				{ name:"主路输出电压线A", key: "mainOutLineA", type: "text", placeholder:"请输入主路输出电压", default:"" },
				{ name:"主路输出电压相A", key: "mainOutPhaseA", type: "text", placeholder:"请输入主路输出电压", default:"" },
				{ name:"主路输出电压线B", key: "mainOutLineB", type: "text", placeholder:"请输入主路输出电压", default:"" },
				{ name:"主路输出电压相B", key: "mainOutPhaseB", type: "text", placeholder:"请输入主路输出电压", default:"" },
				{ name:"主路输出电压线C", key: "mainOutLineC", type: "text", placeholder:"请输入主路输出电压", default:"" },
				{ name:"主路输出电压相C", key: "mainOutPhaseC", type: "text", placeholder:"请输入主路输出电压", default:"" },
				{ name:"主路输出电流相A", key: "mainOutElectricA", type: "text", placeholder:"请输入主路输出电流", default:"" },
				{ name:"主路输出电流相B", key: "mainOutElectricB", type: "text", placeholder:"请输入主路输出电流", default:"" },
				{ name:"主路输出电流相C", key: "mainOutElectricC", type: "text", placeholder:"请输入主路输出电流", default:"" },
				{ name:"供电频率", key: "supplyRate", type: "text", placeholder:"请输入供电频率", default:"" },
				{ name:"负载率 并机≤40% 单机≤80%", key: "loadRate", type: "text", placeholder:"并机≤40% 单机≤80%", default:"" },
				{ name:"电池容量", key: "batteryCapacity", type: "text", placeholder:"请输入电池容量", default:"" },
				{ name:"电池组电压", key: "batteryVoltage", type: "text", placeholder:"请输入电池组电压", default:"" },
				{ name:"电池电流", key: "batteryElectricity", type: "text", placeholder:"请输入电池电流", default:"" },
				{ name:"运行环境", key: "operationTemplate", type: "text", placeholder:"请输入运行环境", default:"" },
				{ name:"异味、异响", key: "abnormalSmell", type: "text", placeholder:"无异味、无异响", default:"" },
				{ name:"电缆绝缘", key: "cable", type: "text", placeholder:"压伤、碰伤、老鼠咬伤", default:"" },
				{ name:"外接电池开关", key: "circumBattery", type: "text", placeholder:"控制箱线缆插接是否牢靠", default:"" },
				{ name:"故障及处理情况", key: "treatment", type: "text", placeholder:"故障及处理情况", default:"" },
				{ name:"建议", key: "advice", type: "text", placeholder:"请输入建议", default:"" },
			]
		} else if (curBar.value == '2') { // 精密空调巡检表
			return [
				{ name:"巡检日期", key: "inspectTime", type: "datetime", placeholder:"", default:"", disabled:true },
				{ name:"设备型号", key: "modelNumber", type: "datetime", placeholder:"", default:"" },
				{ name:"设备序列号", key: "serialNumber", type: "datetime", placeholder:"", default:"" },
				{ name:"运行状态", key: "operation", type: "text", placeholder:"运行或待机", default:"" },
				{ name:"告警信息", key: "alarm", type: "text", placeholder:"有无告警", default:"" },
				{ name:"回风温度冷通道", key: "temperaturePassage", type: "text", placeholder:"冷通道：18~27℃", default:"" },
				{ name:"回风温度辅助区", key: "temperatureAuxiliary", type: "text", placeholder:"辅助区：18~28℃", default:"" },
				{ name:"回风温度电池室", key: "temperatureBattery", type: "text", placeholder:"电池室：20~30℃%", default:"" },
				{ name:"回风湿度冷通道", key: "humidityPassage", type: "text", placeholder:"冷通道：≤60%；", default:"" },
				{ name:"回风湿度辅助区", key: "humidityAuxiliary", type: "text", placeholder:"辅助区：35%~75%", default:"" },
				{ name:"主电源开关", key: "powerSwitch", type: "text", placeholder:"主电源开关状态", default:"" },
				{ name:"过滤网", key: "filter", type: "text", placeholder:"过滤网清洁程度、是否需要更换。", default:"" },
				{ name:"加湿罐/盘", key: "humidification", type: "text", placeholder:"加湿罐清洁程度、是否需要更换", default:"" },
				{ name:"漏水报警器", key: "leak", type: "text", placeholder:"报警是否正常", default:"" },	
				{ name:"温湿度传感器", key: "temperatureHumidity", type: "text", placeholder:"传感器是否正常", default:"" },
				{ name:"冷凝器", key: "condenser", type: "text", placeholder:"冷凝器清洁状态", default:"" },
				{ name:"室内/外风机轴承", key: "bearing", type: "text", placeholder:"轴承是否有异响", default:"" },
				{ name:"室内/外机异味、异响", key: "abnormalSmell", type: "text", placeholder:"无异味、无异响", default:"" },
				{ name:"故障及处理情况", key: "treatment", type: "text", placeholder:"请输入故障及处理情况", default:"" },
				{ name:"建议", key: "advice", type: "text", placeholder:"请输入建议", default:"" },
			]
		} else if (curBar.value == '3') { // 新风系统巡检表
			return [
				{ name:"巡检日期", key: "inspectTime", type: "datetime", placeholder:"", default:"", disabled:true },
				{ name:"运行状态", key: "operation", type: "text", placeholder:"运行或待机", default:"" },
				{ name:"告警信息", key: "alarm", type: "text", placeholder:"有无告警", default:"" },
				{ name:"设定温度", key: "temperature", type: "text", placeholder:"18~28℃", default:"" },
				{ name:"异味、异响", key: "abnormalSmell", type: "text", placeholder:"无异味、无异响", default:"" },
				{ name:"漏水检查", key: "leak", type: "text", placeholder:"无渗漏、无凝露", default:"" },
				{ name:"管路", key: "pipeline", type: "text", placeholder:"是否结露、滴水", default:"" },
				{ name:"过滤网", key: "filter", type: "text", placeholder:"是否脏需要清洗或更换", default:"" },
				{ name:"室外机", key: "outdoorUnit", type: "text", placeholder:"清洁、无异响", default:"" },
				{ name:"故障及处理情况", key: "treatment", type: "text", placeholder:"请输入故障及处理情况", default:"" },
				{ name:"建议", key: "advice", type: "text", placeholder:"请输入建议", default:"" }
			]
		} else if (curBar.value == '4') { // 环境空调巡检表
			return [
				{ name:"巡检日期", key: "inspectTime", type: "datetime", placeholder:"", default:"", disabled:true },
				{ name:"运行状态", key: "mainStatus", type: "text", placeholder:"运行或待机", default:"" },
				{ name:"监控软件状态", key: "supervisoryStatus", type: "text", placeholder:"是否有报警", default:"" },
				{ name:"监控子项目", key: "supervisoryChildStatus", type: "text", placeholder:"监控子项目页面显示是否正常", default:"" },
				{ name:"事件记录", key: "eventRecord", type: "text", placeholder:"是否有异常或告警", default:"" },
				{ name:"时间", key: "time", type: "text", placeholder:"系统时间与标准时间是否一致", default:"" },
				{ name:"对比UPS主机", key: "compareUps", type: "text", placeholder:"显示数据与监控数据是否一致", default:"" },
				{ name:"对比空调主机", key: "compareConditioner", type: "text", placeholder:"显示数据与监控数据是否一致", default:"" },
				{ name:"对比温湿度仪", key: "compareTemperature", type: "text", placeholder:"显示数据与监控数据是否一致", default:"" },
				{ name:"对比漏水报警", key: "compareLeak", type: "text", placeholder:"漏水报警位置及实际是否报警", default:"" },
				{ name:"通讯模块", key: "community", type: "text", placeholder:"通讯模块通讯是否正常、报警延迟是否合理", default:"" },
				{ name:"电话报警", key: "phone", type: "text", placeholder:"测试电话报警是否正常", default:"" },
				{ name:"短信报警", key: "email", type: "text", placeholder:"测试短信报警是否正常", default:"" },
				{ name:"数据备份", key: "data", type: "text", placeholder:"每月对监控软件数据进行备份", default:"" },
				{ name:"故障及处理情况", key: "treatment", type: "text", placeholder:"请输入故障及处理情况", default:"" },
				{ name:"建议", key: "advice", type: "text", placeholder:"请输入建议", default:"" }
			]
		} else if (curBar.value == '5') { // 消防及气体灭火巡检表
			return [
				{ name:"巡检日期", key: "inspectTime", type: "datetime", placeholder:"", default:"", disabled:true },
				{ name:"主机告警系统", key: "mainAlarm", type: "text", placeholder:"主机控制器面板是否有告警信息", default:"" },
				{ name:"主机供电情况", key: "mainElectricity", type: "text", placeholder:"主、备电源供电是否正常", default:"" },
				{ name:"钢瓶压力表压力情况", key: "cylinder", type: "text", placeholder:"红区：缺少压力；绿区：压力正常", default:"" },
				{ name:"应急照明和疏散指示牌", key: "indicator", type: "text", placeholder:"灯亮正常；灯灭异常", default:"" },
				{ name:"温感烟感情况", key: "sense", type: "text", placeholder:"15秒灯闪烁正常；不亮或者常亮异常", default:"" },
				{ name:"手动报警按钮外观", key: "handAlarm", type: "text", placeholder:"手动报警按钮外观是否完好", default:"" },
				{ name:"故障及处理情况", key: "treatment", type: "text", placeholder:"请输入故障及处理情况", default:"" },
				{ name:"建议", key: "advice", type: "text", placeholder:"请输入建议", default:"" }
			]
		} else { //基础环境巡检
			return [
				{ name:"巡检日期", key: "inspectTime", type: "datetime", placeholder:"", default:"", disabled:true },
				{ name:"主机房温湿度", key: "computerTemperature", type: "text", placeholder:"请输入主机房温湿度", default:"" },
				{ name:"配电室温湿度", key: "switchTemperature", type: "text", placeholder:"请输入配电室温湿度", default:"" },
				{ name:"弱电间温湿度", key: "electricityTemperature", type: "text", placeholder:"请输入弱电间温湿度", default:"" },
				{ name:"各设备间地面、墙面、玻璃幕墙洁净度", key: "other", type: "text", placeholder:"请输入各设备间地面、墙面、玻璃幕墙洁净度", default:"" },
				{ name:"机柜情况", key: "cabinetCondition", type: "text", placeholder:"请输入机柜情况", default:"" },
				{ name:"异味、异响", key: "abnormalSmell", type: "text", placeholder:"请输入异味、异响", default:"" },
				{ name:"故障及处理情况", key: "treatment", type: "text", placeholder:"请输入故障及处理情况", default:"" },
				{ name:"建议", key: "advice", type: "text", placeholder:"请输入建议", default:"" },
			]
		}
	})
	
	// 基础环境巡检表单配置
	const initBaseEnvironment = {
		inspectTime:"",
		computerTemperature:"",
		switchTemperature:"",
		electricityTemperature:"",
		other:"",
		cabinetCondition:"",
		abnormalSmell:"",
		treatment:"",
		advice:""
	}
	
	onLoad(async (options) => {
		/* inspecType  0 基础环境  1网络  2服务器 */
		console.log(options);
		rowType.value = options.inspecType
		rowID.value = options.id
		console.log(rowID.value);

		nextTick(async () => {
			await getList()
		})
	})

	/* 基础环境巡检 data */
	const baseEnvironmentVoForm = ref({})

	// 用来存储不同表单的内层id
	let idsMap = new Map()

	const data = reactive({
		rowID: "",
		rowType: "",
		tabsList: [{
			name: "基础环境巡检", value: '0'
		}, {
			name: "UPS巡检表", value: '1'
		}, {
			name: "精密空调巡检表", value: '2'
		}, {
			name: "新风系统巡检表", value: '3'
		}, {
			name: "环境空调巡检表", value: '4'
		}, {
			name: "消防及气体灭火巡检表", value: '5'
		}],
		curBar: "0",
		form: {}
	})
	const {
		rowID,
		rowType,
		tabsList, form, curBar
	} = toRefs(data)
	/* tabs 切换 */
	const tabsClick = async (item) => {
		curBar.value = item.value
		getForm()
	}

	const getList = async () => {
		if (rowType.value == 0) {
			await getBaseDetail()
		} else {
			await getServiceNetDetail()
		}
	}
	const getServiceNetDetail = async () => {
		const res = await inspaServiceNetDetail(rowID.value)
		console.log(res);
	}
	const getBaseDetail = async () => {
		const res = await inspaBaseDetail(rowID.value)
		console.log(res);
		if(res.code != 200) return
		let data = res.data
		idsMap.set("0", data.baseEnvironmentVo.id) //基础环境巡检
		idsMap.set("1", data.upsVo.id) //UPS巡检表
		idsMap.set("2", data.airConditionerVo.id) //精密空调巡检表
		idsMap.set("3", data.newTrendVo.id) //新风系统巡检表
		idsMap.set("4", data.environmentControlVo.id) //环境空调巡检表
		idsMap.set("5", data.alarmFireVo.id) //消防及气体灭火巡检表
		getForm()
	}
	
	async function getForm() {
		console.log("idsMap.has(curBar.value)", idsMap.has(curBar.value))
		if(!idsMap.has(curBar.value)) return;
		const res = await getFormInfoAPI(idsMap.get(curBar.value), curBar.value)
		console.log(`获取到的信息${curBar.value}`, res)
		form.value = {}
		if(res.code != 200) return;
		form.value = { ...res.data }
	}
	
	/** 提交当前表单 */
	async function submit() {
		try{
			configList.value.forEach(config=>{
				if(!form.value[config.key]) {
					throw new Error(`${config.name}未填写`)
				}
			})
		}catch(e){
			//TODO handle the exception
			uToastRef.value.show({
				type: 'warning',
				message: e.message
			})
			return
		}
		
		const res = await updataFormInfoAPI(form.value, curBar.value)
		if(res.code == 200) {
			uToastRef.value.show({
				type: 'success',
				message: "上报成功"
			})
			loop = true
			getForm()
		}
	}

	function back() {
		uni.navigateBack(-1)
		if(loop) {
			uni.setStorageSync("reget", true)
		}
	}
</script>

<style scoped lang="scss">
	.page_top {
		width: 100%;
		height: calc(var(--status-bar-height) + 88rpx);
		padding-top: var(--status-bar-height);
		background-color: #f8f8f8;
		position: relative;

		.page_top_conetnt {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
			position: relative;
		}

		.back_logo {
			--back-img-size: 40rpx;
			width: var(--back-img-size);
			height: var(--back-img-size);
			position: absolute;
			top: 50%;
			left: 30rpx;
			transform: translateY(-50%);
		}

		.submit_text {
			position: absolute;
			right: 30rpx;
			color: $uni-color-primary;
			font-size: 24rpx;
			top: 50%;
			transform: translateY(-50%);
		}
	}
	.scroll_box {
		width: 100%;
		height: calc(100vh - var(--status-bar-height) - 88rpx - 44px);
		.scroll_content {
			padding: 30rpx;
		}
	}

	.form-content {
		padding: 30rpx;
		box-sizing: border-box;
	}
</style>